package com.slfx.idgen.demo.idgen.config;

import com.slfx.idgen.demo.idgen.properties.UUIDGeneratorProperties;
import com.slfx.idgen.demo.idgen.uuid.CachedUUIDGenerator;
import com.slfx.idgen.demo.idgen.uuid.UUIDGenerator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(UUIDGeneratorProperties.class)
public class UUIDGeneratorAutoConfiguration {
    private UUIDGeneratorProperties generatorProperties;

    public UUIDGeneratorAutoConfiguration(UUIDGeneratorProperties generatorProperties) {
        this.generatorProperties = generatorProperties;
    }

    @Bean("cachedUUIDGenerator")
    @ConditionalOnProperty(prefix = "idgen.uuid", name = "cached", havingValue = "true")
    public CachedUUIDGenerator initCachedUUIDGenerator() {
        return new CachedUUIDGenerator(generatorProperties.getMaxSize());
    }

    @Bean("uUIDGenerator")
    public UUIDGenerator initUUIDGenerator() {
        return new UUIDGenerator();
    }
}
